package cn.wolfcode.shop.cloud.redis;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.redis.util.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

@Setter
@Getter
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    //1、setter方法
    public <T>void set(KeyPrefix keyPrefix,String key,T data){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            //判断是否有过期时间
            if(keyPrefix.getExpireTime()>0){
                jedis.setex(realKey, keyPrefix.getExpireTime(), JSON.toJSONString(data));
            }else{
                jedis.set(realKey, JSON.toJSONString(data));
            }

        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //2、getter方法
    public <T>T get(KeyPrefix keyPrefix,String key,Class<T> clz){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            String jclz = jedis.get(realKey);
            T t = JSON.parseObject(jclz, clz);
            return t;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //3、incr
    public <T>long incr(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            Long incr = jedis.incr(realKey);
            return incr;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //4、decr
    public <T>long dect(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            Long decr = jedis.decr(realKey);
            return decr;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //5、exists
    public boolean exists(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            Boolean exists = jedis.exists(realKey);
            return exists;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //6、expire
    public <T>long expire(KeyPrefix keyPrefix,String key,int secoends){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            Long expire = jedis.expire(realKey,secoends);
            return expire;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //7、hset方法
    public <T>void hset(KeyPrefix keyPrefix,String key,String field,T data){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            //判断是否有过期时间
                jedis.hset(realKey, field, JSON.toJSONString(data));
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //8、hget方法
    public <T>T hget(KeyPrefix keyPrefix,String key,String field,Class<T> clz){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            String jclz = jedis.hget(realKey, field);
            T t = JSON.parseObject(jclz, clz);
            if(t==null){
                return null;
            }
            return t;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //9、hgetAll方法：
    public <T>Map<String,T> hgetAll(KeyPrefix keyPrefix,String key,Class<T> clz){
        try(Jedis jedis = jedisPool.getResource()) {//会自动释放资源
            String realKey = keyPrefix.getKey()+key;
            Map<String, String> stringMap = jedis.hgetAll(realKey);
            //将map中的value类型改为T类型
            Map<String,T> map = new HashMap<>();
            for(Map.Entry<String,String> stringMapEntry:stringMap.entrySet()){
                map.put(stringMapEntry.getKey(),JSON.parseObject(stringMapEntry.getValue(),clz));
            }
            return map;
        }catch (Exception e){
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

}
